package com.aidex.biz.controller;

import java.util.Date;
import java.util.List;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.utils.StringUtils;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.YushenInfo;
import com.aidex.biz.service.YushenInfoService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 客户预审Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-12-08
 */
@RestController
@RequestMapping("/biz/bizYushenInfo")
public class YushenInfoController extends BaseController
{
    @Autowired
    private YushenInfoService yushenInfoService;

    /**
     * 查询客户预审列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(YushenInfo yushenInfo, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                yushenInfo.setCompanyId(null);
        }else{
                     yushenInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        yushenInfo.setPage(new PageDomain(request, response));
        return R.data(yushenInfoService.findPage(yushenInfo));
    }

    /**
     * 获取客户预审详细信息
     */
    @GetMapping(value = "/{id}")
    public R<YushenInfo> detail(@PathVariable("id") String id)
    {
        return R.data(yushenInfoService.get(id));
    }

    /**
     * 新增客户预审
     */
    @Log(title = "客户预审", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  YushenInfo yushenInfo)
    {
        yushenInfo.setId(StringUtils.Getnum());
        yushenInfo.setIsNewRecord();
        yushenInfo.setCreateTime(new Date());
        return R.data(yushenInfoService.save(yushenInfo)?yushenInfo:null);
    }

    /**
     * 修改客户预审
     */
    @Log(title = "客户预审", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated YushenInfo yushenInfo)
    {
        return R.status(yushenInfoService.save(yushenInfo));
    }


    /**
     * 删除客户预审
     */
    @Log(title = "客户预审", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(yushenInfoService.deleteYushenInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(yushenInfoService.findMaxSort(new YushenInfo()));
    }

    /**
     * 导出客户预审列表
     */
    @Log(title = "客户预审", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(YushenInfo yushenInfo)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    yushenInfo.setCompanyId(null);
            }else{
                         yushenInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<YushenInfo> list = yushenInfoService.findList(yushenInfo);
        ExcelUtil<YushenInfo> util = new ExcelUtil<YushenInfo>(YushenInfo.class);
        return util.exportExcel(list, "客户预审数据");
    }

}
